package com.example.orderfoodsystem22201310liuziming.controller;

import com.example.orderfoodsystem22201310liuziming.entity.foodmenu;
import com.example.orderfoodsystem22201310liuziming.repository.CartRepository;
import com.example.orderfoodsystem22201310liuziming.entity.shoppingcart;
import com.example.orderfoodsystem22201310liuziming.repository.MenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Optional;

@Controller
@RequestMapping("/cart")
public class ShoppingCartController {
    @Autowired //接线
    private CartRepository cartRepository;
    @Autowired
    private MenuRepository menuRepository;
    @Autowired
    private HttpSession httpSession;

    @RequestMapping("/deletecartfood/{Id}")  //删除购物车菜品
    protected String deleteCartFood(@PathVariable("Id") Integer id){
        Optional<shoppingcart> deleteCartFood = cartRepository.findById(id);
        if(deleteCartFood.isEmpty()){
            System.out.println("不存在记录");
        }
        cartRepository.deleteById(id);
        return "redirect:/buyer/menulist";
    }

    @RequestMapping("/reducecartfood/{Id}")  //减少购物车菜品
    protected String reduceCartFood(@PathVariable("Id") Integer id){
        Optional<shoppingcart> reduceCartFood = cartRepository.findById(id);
        if(reduceCartFood.isEmpty()){
            System.out.println("不存在记录");
        } else {
            shoppingcart shoppingcart1 = reduceCartFood.get();
            if(shoppingcart1.getFoodnumber() > 1){
                shoppingcart1.setFoodnumber(shoppingcart1.getFoodnumber() - 1);
                cartRepository.save(shoppingcart1);
            }
        }
        return "redirect:/buyer/menulist";

    }

    @RequestMapping("/addCart/{menuId}") //菜单添加到购物车
    protected String addCart(@PathVariable Integer menuId){
        Optional<foodmenu> foodmenuOptional = menuRepository.findById(menuId);
        foodmenu foodmenu1 = foodmenuOptional.get();

        String username = (String)httpSession.getAttribute("aaaa");
        shoppingcart shoppingcartOptional = cartRepository.findByUsernameAndFoodID(username, menuId);
        if(null==shoppingcartOptional && foodmenu1.getFoodstatus()==1){
            shoppingcart shoppingcart1 = new shoppingcart();
            shoppingcart1.setFoodID(foodmenu1.getId());
            shoppingcart1.setFoodname(foodmenu1.getFoodname());
            shoppingcart1.setFoodprice(foodmenu1.getFoodprice());
            shoppingcart1.setFoodnumber(1);
            shoppingcart1.setUsername(username);
            cartRepository.save(shoppingcart1);
        }
        else if(foodmenu1.getFoodstatus()==1){
            shoppingcart a = shoppingcartOptional;
            a.setFoodnumber(a.getFoodnumber()+1);
            cartRepository.save(a);
        }

        return "redirect:/buyer/menulist";
    }



//----------------------------------------------------------------------------
    @RequestMapping(value = "/shoppingCartJson")
    @ResponseBody
    protected Iterable<shoppingcart> shoppingCartJson(){
        String username = (String)httpSession.getAttribute("aaaa");
        return cartRepository.findAllByUsername(username);
    }
    @RequestMapping(value = "/menuListJson")
    @ResponseBody
    protected Iterable<foodmenu>menuListJson(){
        return menuRepository.findAll();
    }
}
